package com.github.binarywang.demo.wx.mp.handler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.github.binarywang.demo.wx.mp.config.WxMpConfiguration;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import io.renren.modules.app.entity.KafKaMsgJxt;
import io.renren.modules.app.entity.WxKafKaMsg;
import jodd.jerry.Jerry;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

/**
 * @author MrBird
 */
@Slf4j
@Component
public class KafkaMessageListener {
	
	@KafkaListener(topics = "test", groupId = "test-consumer")
	public void listen(String message) throws WxErrorException {
		log.info("接收消息: {}", message);
		WxKafKaMsg msgKafka = JSONObject.parseObject(message, WxKafKaMsg.class);
		WxMpService mpService = WxMpConfiguration.getMpServices().get(msgKafka.getAppId());
		
		String templateId = "pWJ71mL3freLtz5-DMxuqt6sHoEDRs-se732MtV7bGc";
		
		List<WxMpTemplateData> data = new ArrayList<>();
		data.add(new WxMpTemplateData("first","审核提醒","#173177"));
		String href = Jerry.jerry(msgKafka.getContent()).$("a").attr("href");
		String remark = Jerry.jerry(msgKafka.getContent()).$("a").text();
		data.add(new WxMpTemplateData("keyword1",remark,"#173177"));//访问地址 href
		data.add(new WxMpTemplateData("keyword2","","#173177"));//IMEI号
		data.add(new WxMpTemplateData("keyword3",DateUtil.formatDateTime(new Date()),"#173177"));//处理时间
		data.add(new WxMpTemplateData("remark",remark,"#173177"));//备注
		
		WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage(msgKafka.getOpenId(), templateId, href, null, data);
		String sendTemplateMsg = mpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
		log.info("发送结果:{}",sendTemplateMsg);
	}
	
	@KafkaListener(topics = "jxt", groupId = "test-consumer")
	public void listenJxt(String message) throws WxErrorException {
		log.info("接收jxt消息: {}", message);
		KafKaMsgJxt msgKafka = JSONObject.parseObject(message, KafKaMsgJxt.class);
		Map<String, Object> params = msgKafka.getParams();
		WxMpService mpService = WxMpConfiguration.getMpServices().get(msgKafka.getAppId());
		
		List<WxMpTemplateData> data = new ArrayList<>();
		
		ValueCss vs = JSONObject.parseObject(Convert.toStr(params.get("first")),ValueCss.class);
		data.add(new WxMpTemplateData("first",vs.getValue(),vs.getColor()));
		
		vs = JSONObject.parseObject(Convert.toStr(params.get("keyword1")),ValueCss.class);
		data.add(new WxMpTemplateData("keyword1",vs.getValue(),vs.getColor()));//访问地址
		
		vs = JSONObject.parseObject(Convert.toStr(params.get("keyword2")),ValueCss.class);
		data.add(new WxMpTemplateData("keyword2",vs.getValue(),vs.getColor()));//IMEI号
		
		vs = JSONObject.parseObject(Convert.toStr(params.get("keyword3")),ValueCss.class);
		data.add(new WxMpTemplateData("keyword3",vs.getValue(),vs.getColor()));//处理时间
		
		vs = JSONObject.parseObject(Convert.toStr(params.get("remark")),ValueCss.class);
		data.add(new WxMpTemplateData("remark",vs.getValue(),vs.getColor()));//备注
		
		WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage(msgKafka.getOpenId(), msgKafka.getTemplateId(), msgKafka.getUrl(), null, data);
		String sendTemplateMsg = mpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
		log.info("发送结果:{}",sendTemplateMsg);
	}
	
	@Data
	@AllArgsConstructor
	public static class ValueCss{
		private String value;
		private String color;
	}
}